package com.ctcemti.mdm.project.sys.controller;

import com.ctcemti.mdm.common.result.Result;
import com.ctcemti.mdm.common.result.ResultEnum;
import com.ctcemti.mdm.common.util.JWTUtil;
import com.ctcemti.mdm.project.sys.entity.OrgOrganization;
import com.ctcemti.mdm.project.sys.entity.OrgPerson;
import com.ctcemti.mdm.project.sys.entity.OrgRole;
import com.ctcemti.mdm.project.sys.service.IOrgPersonService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @ClassName SysMenuController
 * @Description 专业领域控制层
 * @Author zpw
 * @Date 11:50 2019/7/16
 * @Version V1.0
 **/
@RestController
@Api(value = "OrgPersonController", description = "专业领域控制器类")
@RequestMapping("orgPerson")
public class OrgPersonController {
    @Autowired
    private IOrgPersonService orgPersonService;


    @ApiOperation(value = "新增人员")
    @PostMapping("insert")
    @ApiImplicitParam(value="OrgPerson",name = "人员实体类")
    public Result<OrgPerson> insertInfDisciplines(OrgPerson orgPerson, @RequestParam("file")MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws IOException {
        String loginId = JWTUtil.getLoginId(request.getHeader("Authorization"));
        orgPerson.setCreator(loginId);
        return orgPersonService.insert(orgPerson,file);
    }

    @ApiOperation(value = "分页查询")
    @PostMapping("selectAllOrgPerson")
    @ApiImplicitParam(value="orgId",name = "组织ID")
    public Result<OrgPerson> selectAllOrgPerson(OrgPerson orgPerson, HttpServletRequest request, HttpServletResponse response){
        PageHelper.startPage(orgPerson.getPageIndex(), orgPerson.getPageSize(),"d.sort");
//        PageHelper.startPage(orgPerson.getPageIndex(), orgPerson.getPageSize(),"create_date");
        List<OrgPerson> list = orgPersonService.selectAllOrgPerson(orgPerson);
        for (OrgPerson person : list) {
            String str="";
            for (OrgRole orgRole : person.getRoleList()) {
                str+=orgRole.getName()+",";
            }
            if(str!=""){
                str=str.substring(0, str.length()-1);
            }
            person.setRoleName(str);
        }
        PageInfo<OrgPerson> pagelist = new PageInfo<OrgPerson>(list);
        return new Result<OrgPerson>(ResultEnum.SUCCESS,pagelist);
    }

    @ApiOperation(value = "分页查询(1:排除已绑定角色的人员或者2：查寻已绑定的人员)")
    @PostMapping("selectAllOrgPersonNoRoleOrHasRole")
    public Result<OrgPerson> selectAllOrgPersonNoRoleOrHasRole(OrgPerson orgPerson,Integer flag, HttpServletRequest request, HttpServletResponse response){
        PageHelper.startPage(orgPerson.getPageIndex(), orgPerson.getPageSize(),"d.sort");
//        PageHelper.startPage(orgPerson.getPageIndex(), orgPerson.getPageSize(),"create_date");
        List<OrgPerson> list = orgPersonService.selectAllOrgPersonNoRoleOrHasRole(orgPerson,flag);
        PageInfo<OrgPerson> pagelist = new PageInfo<OrgPerson>(list);
        return new Result<OrgPerson>(ResultEnum.SUCCESS,pagelist);
    }

    @ApiOperation(value = "根据id查询人员")
    @PostMapping("selectOrgPerson")
    @ApiImplicitParam(value="id",name = "人员ID")
    public Result<OrgPerson> selectOrgPerson(OrgPerson orgPerson, HttpServletRequest request, HttpServletResponse response){
        List<OrgPerson> list = orgPersonService.selectOrgPerson(orgPerson);
        List<OrgRole> roleList = orgPersonService.selectOrgRole(orgPerson);
        String names="";
        String ids="";
        for (int i=0;i<roleList.size();i++) {
            ids+=roleList.get(i).getId();
            names+=roleList.get(i).getName();
            if(i!=roleList.size()-1){
                ids+=",";
                names+=",";
            }
        }
        list.get(0).setRoleId(ids);
        list.get(0).setRoleName(names);
        return new Result<OrgPerson>(ResultEnum.SUCCESS,list);
    }

    @ApiOperation(value = "根据id查询人员")
    @PostMapping("selectOrgPersonByIds")
    @ApiImplicitParam(value="ids",name = "人员IDs")
    public Result<OrgPerson> selectOrgPersonByIds(String ids, HttpServletRequest request, HttpServletResponse response){
        List<OrgPerson> list = orgPersonService.selectOrgPersonByIds(ids);
        return new Result<OrgPerson>(ResultEnum.SUCCESS,list);
    }


    @ApiOperation(value = "修改")
    @PostMapping("updateOrgPerson")
    @ApiImplicitParam(value="OrgPerson",name = "人员实体类")
    public Result<OrgPerson> updateOrgPerson(OrgPerson orgPerson, @RequestParam("file")MultipartFile file, HttpServletRequest request, HttpServletResponse response){
        String loginId = JWTUtil.getLoginId(request.getHeader("Authorization"));
        orgPerson.setModifiedBy(loginId);
        orgPerson.setModifyDate(new Date());
        return orgPersonService.updateOrgPerson(orgPerson,file);
    }

    @ApiOperation(value = "删除")
    @PostMapping("deleteOrgPerson")
    @ApiImplicitParam(value="id",name = "人员ID")
    public Result<OrgPerson> deleteOrgPerson(  String[]  ids, HttpServletRequest request, HttpServletResponse response){
        int num = orgPersonService.deleteOrgPerson(ids);
        return new Result<OrgPerson>(ResultEnum.SUCCESS,num);
    }

    @ApiOperation(value = "登录名是否存在")
    @PostMapping("hasOrgPerson")
    @ApiImplicitParam(value="loginName",name = "登录名")
    public Result<OrgPerson> hasOrgPerson(OrgPerson orgPerson, HttpServletRequest request, HttpServletResponse response){
        int num = orgPersonService.hasOrgPerson(orgPerson);
        return new Result<OrgPerson>(ResultEnum.SUCCESS,num);
    }

    @ApiOperation(value = "查询组织树")
    @PostMapping("selectOrgTree")
    public Result<OrgOrganization> selectOrgTree(OrgPerson orgPerson, HttpServletRequest request, HttpServletResponse response){
        List<OrgOrganization> list =orgPersonService.selectOrgTree(orgPerson,request.getHeader("Authorization"));
        return new Result<OrgOrganization>(ResultEnum.SUCCESS,list);
    }


    @ApiOperation(value = "移动")
    @PostMapping("moveOrgPerson")
    @ApiImplicitParams({
            @ApiImplicitParam(value="ids",name = "人员id"),
            @ApiImplicitParam(name="orgids",value="组织id"),
            @ApiImplicitParam(name="retain",value="是否保留原原组织关系")
    })
    public Result<OrgOrganization> moveOrgPerson(@RequestParam(value = "ids")  String[]  ids,
                                                 @RequestParam(value = "orgids")  String[]  orgids,
                                                 @RequestParam(value = "retain")  Boolean retain,
                                                 HttpServletRequest request, HttpServletResponse response){
        int  num =orgPersonService.moveOrgPerson(ids,orgids,retain);
        return new Result<OrgOrganization>(ResultEnum.SUCCESS,num);
    }

    @ApiOperation(value = "数据导入")
    @PostMapping("uploadFile")
    @ApiImplicitParams({
            @ApiImplicitParam(value="orgId",name = "组织id")
    })
    public Result<Map<String,Object>> uploadFile(OrgPerson orgPerson, HttpServletRequest request, HttpServletResponse response) throws Exception {
        CommonsMultipartResolver commonsMultipartResolver =  new CommonsMultipartResolver(request.getSession().getServletContext());
        MultipartFile multipartFile = null;
        if(commonsMultipartResolver.isMultipart(request)){
            MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest)request;
            Iterator<String> iterator = multipartHttpServletRequest.getFileNames();
            while (iterator.hasNext()){
                multipartFile= multipartHttpServletRequest.getFile(iterator.next());
            }
        }
        Map<String,Object> result = orgPersonService.uploadFile(multipartFile,orgPerson.getOrgId(),response,request);
        return new Result<Map<String,Object>>(ResultEnum.SUCCESS,result);
    }
}
